package com.yonyou.wdt.job;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yonyou.wdt.entity.QueryDto;
import com.yonyou.wdt.job.config.BaseConfig;
import com.yonyou.wdt.service.YonsuiteService;
import com.yonyou.wdt.utils.RedisUtils;
import com.yonyou.wdt.utils.YsQueryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

@Component
public class YsCustToRedisJob extends BaseConfig {
    private static Logger logger = LoggerFactory.getLogger(YsProductToRedisJob.class);
    @Resource
    private YonsuiteService yonsuiteService;
    private static final String JOB_CLASS="maoEr000";

    @XxlJob("ysCustToRedisJob")
    public ReturnT<String> ysCustToRedisJob() {


        // 处理相关业务
        QueryDto dto = new QueryDto();
        dto.setTenantId(getYsTenantId());
        dto.setAppKey(getYsAppKey());
        dto.setAppSecret(getYsAppSecret());
        // 获取YS系统租户动态域名信息
        ReturnT<String> isReturn2 = getYsDynamicDomainName(JOB_CLASS,dto);
        // 域名获取失败
        if (isReturn2 != null) return isReturn2;
        try {
                // 1、客户列表查询，查询出所有客户，将id与code拼装成一个map集合，进行比对判断
                HashMap<String, Object> queryMap = new HashMap<>();  // 客户档案列表查询请求参数Map
                ArrayList<JSONObject> tBcustList = new ArrayList<>();  // 2B客户档案列表查询返回值
                ArrayList<JSONObject> custList = new ArrayList<>();  // 2B客户档案列表查询返回值
                int pageIndex = 1;
                while (true) {
                    queryMap.put("pageIndex", pageIndex);
                    queryMap.put("pageSize", 500);
                    JSONObject wareJsonList = yonsuiteService.getMerchantList(queryMap, dto);
                    if (wareJsonList.getStr("code").equals("200")) {
                        if (null != wareJsonList.getJSONArray("data")) {
                            JSONArray custJSONArray = wareJsonList.getJSONArray("data");
                            for (Object custObj : custJSONArray) {
                                JSONObject entries = JSONUtil.parseObj(custObj);
                                custList.add(entries);
                                JSONObject jsonObject = entries.getJSONObject("merchantCharacter");
                                if (jsonObject!=null && Boolean.TRUE.equals(jsonObject.getBool("maoer02"))){
                                    tBcustList.add(entries);
                                }
                            }
                        }
                    }else {
                        break;
                    }
                    pageIndex++;
                }
                logger.info("YS系统客户档案查询完成共：{}个客户,2B客户档案查询完成共：{}个客户", custList.size(),tBcustList.size());
               if (!tBcustList.isEmpty()){
                   RedisUtils.set(dto.getTenantId() + ":2BCust", tBcustList);
               }
            if (!custList.isEmpty()){
                for (JSONObject entries : custList) {
                    RedisUtils.set(dto.getTenantId() + ":cust:"+entries.getStr("code"), entries);
                }
            }
            InsertLogger(JOB_CLASS,null, "1", "YS系统客户档案查询完成共，共：{" + custList.size() + "}条,其中2B客户档案查询完成共，共：{" + tBcustList.size() + "}条");

        } catch (Exception e) {
            XxlJobHelper.handleFail(e.getMessage());
            InsertLogger(JOB_CLASS,null, "0", e.getMessage());
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }


}
